package com.itboye.textileexpres.order;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.textileexpes.beans.OrderDeltilteBeans;
import com.itboye.textileexpes.presenter.MyOrderPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.alipaytest.ActivityPaymentMethod;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.MainApplication;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.base.WebActivity;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.TimeFormat;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexptes.config.GlobalConfig;

/**
 * 
 * @author 代付款付款詳情
 * 
 */
public class ActivityDaiFuKuan extends BaseActivity implements OnClickListener,
		Observer {
	TextView title;
	ImageView back, img_other;
	String type;
	MyOrderPresenter orderPresenter;
	OrderDeltilteBeans beans;
	String order_code;
	private TextView order_num;
	private TextView create_time;
	String addre;
	private TextView tvshouhuo, textPhone, textPadd, faburen, cole, tvyunfei,
			tvprice;

	private LinearLayout litouytDetait;

	// 传过来的
	private String str;
	private TextView shengyutime;
	
	private Button btn_sendOk,btnWuliu;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_daifukuan;
	}

	@Override
	public void onMyCreate() {
		title.setText("订单详情");
		img_other.setVisibility(View.GONE);
		// TODO Auto-generated method stub
		cole.setText("交易成功");
		order_code = getIntent().getStringExtra("order_code");
		System.out.println("order_code_code" + order_code);
		str = getIntent().getStringExtra("str");
		System.out.println("strstr" + str);
		beans = new OrderDeltilteBeans();
		type = getIntent().getStringExtra("type");
		orderPresenter = new MyOrderPresenter(this);
		orderPresenter.getorder(IsMobileNumPhone.isUid(), order_code);
		onTime();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.btnWuliu:
			Intent intent = new Intent(MainApplication.context, WebActivity.class);
			intent.putExtra(
					"url",
					"http://api.guannan.itboye.com/public/web.php/Express/index?order_code="
							+ beans.getOrder_code() + IsMobileNumPhone.isUid()
							+ IsMobileNumPhone.isPasswrd());
			intent.putExtra("title", "物流查看");
			startActivity(intent);
			
			break;
		case R.id.btn_sendOk:
			 intent=new Intent(MainApplication.context,ActivityPaymentMethod.class);
			intent.putExtra("price",beans.getPrice());
			intent.putExtra("orderNum", beans.getOrder_code());
			startActivity(intent);
			break;

		default:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == orderPresenter.Xiang_success) {
				beans = (OrderDeltilteBeans) entity.getData();
				System.out.println("notenote"
						+ beans.getItems().get(0).getName());
				getCounte(beans);
				onAddres(beans);
			} else if (entity.getEventType() == orderPresenter.Delete_success) {
				toast(entity.getData() + "");

			}
		}
	}

	/**
	 * 计算
	 */
	public void onTime() {
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = TimeFormat.DateFormat(beans.get_auto_op_time());
		java.util.Date begin = null;
		try {
			begin = dfs.parse(TimeFormat.getSimData());
		} catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		java.util.Date end = null;
		try {
			end = dfs.parse(time);

		} catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒

		long day1 = between / (24 * 3600);
		long hour1 = between % (24 * 3600) / 3600;
		long minute1 = between % 3600 / 60;
		long second1 = between % 60 / 60;
		if (day1 <= 0 || hour1 <= 0 || minute1 <= 0) {
			shengyutime.setText("交易关闭");
		} else {
			shengyutime.setText("" + day1 + "天" + hour1 + "小时" + minute1 + "分");
		}

	}

	private void onAddres(OrderDeltilteBeans beans) {
		if (beans.get_address() == null) {
			return;
		} else {
			addre = beans.get_address().getCountry()
					+ beans.get_address().getProvince()
					+ beans.get_address().getDetailinfo();
			tvshouhuo.setText(beans.getContactname());
			textPhone.setText(beans.get_address().getMobile());
			textPadd.setText(addre);

		}
	}

	private void getCounte(OrderDeltilteBeans beans) {
		for (int i = 0; i < beans.getItems().size(); i++) {

			faburen.setText("发布人:" + beans.getPublisher_name());

			order_num.setText(order_code);
			View view = LayoutInflater.from(ActivityDaiFuKuan.this).inflate(
					R.layout.item_order_detail, null);
			TextView goods_name1_tv = (TextView) view
					.findViewById(R.id.goods_name1_tv);
			TextView goods_name2_tv = (TextView) view
					.findViewById(R.id.goods_name2_tv);
			TextView goods_detail_tv = (TextView) view
					.findViewById(R.id.goods_detail_tv);
			TextView color_tv = (TextView) view.findViewById(R.id.color_tv);
			TextView price_tv = (TextView) view.findViewById(R.id.price_tv);
			TextView num_tv = (TextView) view.findViewById(R.id.num_tv);

			goods_name1_tv.setText(beans.getPublisher_name());
			goods_name2_tv.setText(beans.getItems().get(i).getName());
			goods_detail_tv.setText(beans.getPublisher_name());
			color_tv.setText(beans.getItems().get(i).getSku_desc());
			price_tv.setText(beans.getItems().get(i).getPrice());
			num_tv.setText(beans.getItems().get(i).getCount());

			ImageView goods_img = (ImageView) view.findViewById(R.id.goods_img);
			create_time.setText(TimeFormat.DateFormat(beans.getCreatetime()));
			tvyunfei.setText(beans.getItems().get(i).getOri_price() + "元");
			tvprice.setText(beans.getItems().get(i).getDt_origin_country());
			XImageLoader.load(
					GlobalConfig.IMAGE_DOUBLE
							+ beans.getItems().get(i).getImg(), goods_img);
			MarginLayoutParams layoutParams = new MarginLayoutParams(
					MarginLayoutParams.WRAP_CONTENT,
					MarginLayoutParams.WRAP_CONTENT);
			layoutParams.setMargins(5, 5, 5, 5);
			litouytDetait.addView(view, layoutParams);

		}
	}
}
